import logging
import os
import time

from image import Image


class ImageReference(Image):
    def __init__(self, abs_path: str, rel_path: str, md_file_name: str):
        """
        初始化一个 ImageReference 对象

        :param abs_path: 图片的绝对路径
        :param rel_path: 图片的相对路径
        :param md_file_name: 引用该图片的 Markdown 文件的名称
        """
        super().__init__(abs_path)
        self.rel_path = rel_path
        self.md_file_name = md_file_name

    def __str__(self):
        """
        返回用户友好的字符串表示，用于调试和日志输出
        """
        return (f"ImageReference(name='{self.name}', "
                f"abs_path='{self.abs_path}', "
                f"rel_path='{self.rel_path}', "
                f"md_file_name='{self.md_file_name}')")


    def update(self):
        timestamp = time.time_ns()
        _, ext = os.path.splitext(self.name)
        self.name = f"{self.md_file_name}-{timestamp}{ext}"
        self.update_abs_path()
        self.update_rel_path()
        logging.info(self)

    def update_rel_path(self):
        """
        更新图片的相对路径

        """

        new_path = os.path.join(os.path.dirname(self.rel_path), self.name)
        self.rel_path = new_path
